// Copyright (C) 2005 Taylor L. Riché <riche@cs.utexas.edu>
//  
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software 
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
// 	$Id: ModulePrinter.java 354 2005-11-07 03:31:53Z richetl $	
//

import java.io.*;

class ModulePrinter
{
    public void print(String modName, FuncExits ex, Events ev, CppIncludes i,
		      CppMethods m, AppDecls d, AppWirings w, boolean genStubs) {
	
	String filename = "llt_" + modName + ".hh";
	FileOutputStream out = null;
	PrintStream file = null;
	try {
	    out = new FileOutputStream(filename);
	    file = new PrintStream(out);
	}
	catch(FileNotFoundException e) {
	    System.err.println("Specified file " + filename + " does not exist.");
	    System.exit(1);
	}
	if(ev == null) {
	    ev = new Events();
	}

	// Print guards
	file.println("#ifndef LLT_" + modName.toUpperCase() + "_H");
	file.println("#define LLT_" + modName.toUpperCase() + "_H");
	file.println();

	// Automatic includes
	file.println("#include <assert.h>");
	file.println("#include <lee/Module.hh>");

	// Include generated ppf header files
	d.printIncludes(file);

	// Print namespace
	file.println("namespace userFunction {");
	file.println();

	file.println("class " + modName + " : public lee::Module");
	file.println("{");

	// Print the programmer methods
	if(m != null) {
	    m.print(file);
	}

	// Define formInnerModules

	file.println("\tvoid formInnerComponents(Container * cont) {");

	// Add exits to the list
	if(ex != null) {
	    ex.printMod(file);
	}
	file.println();

	// Create the declared ppfs

	d.print(file);

	// Create the wirings

	w.setEnv(d.getEnv());
	w.printMod(modName, file);

	file.println("\t}");

	if(ev != null) {
	    ev.printSubscribeSwitch(file);
	    ev.printPublish(file);
	    ev.printSubscribe(file);
	}

	file.println("public:");

	file.println("\t" + modName + "() {}");
	file.println("\t" + modName + "(std::string s, std::string t) : Module(s,t) {}");
	file.println("\tvirtual ~" + modName + "() {}");
	
	// Print the constants defined by the exit points
	if(ex != null) {
	    ex.printPubs(file);
	}

	// Close class and guards
	file.println("};");
	file.println("}");
	file.println("#endif");
	file.println();

    }

}

